home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12711 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.1 KB  |  70 lines

  1. Path: lrz-muenchen.de!news
  2. From: watzka@stat.uni-muenchen.de (Kurt Watzka)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: help with strcmp
  5. Date: 2 Apr 1996 12:17:31 GMT
  6. Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
  7. Distribution: world
  8. Message-ID: <4jr5sr$msn@sparcserver.lrz-muenchen.de>
  9. References: <4jpiek$lp6@blaze.cs.jhu.edu>
  10. NNTP-Posting-Host: sun2.lrz-muenchen.de
  11.  
  12. lasher@hops.cs.jhu.edu (John E. Davis) writes:
  13.  
  14. >For some reason when I try to compare the strings in the following 
  15. >snippet I consistently get a core dump (running on a UNIX machine running 
  16. >Solaris).  Could anyone point out what may be going wrong?  I have run it 
  17. >through the debugger and that was no help at all for me.  here is the 
  18. >code snippet:
  19.  
  20. >#include <stdio.h>
  21. >#include <stdlib.h>
  22. >#include <string.h>
  23.  
  24. >void setup(FILE *);
  25.  
  26. >void main(int argc, char *argv[])
  27.  
  28. No comment!
  29.  
  30. >{
  31. >char         buf[20], data[40], *buff, *arg, *fp;
  32. >FILE         *handle, *dest;
  33. >int         n;
  34.  
  35. >handle = fopen(argv[1], "r");
  36.  
  37. You should not try to use "argv[1]" like that without making sure
  38. that argc is at least 2. 
  39.  
  40. >if(dest = fopen( "dbuild.out", "w")) setup(dest);
  41.  
  42. You should not use a FILE * from fopen() without _first_ checking
  43. wether the fopen() call suceeded.
  44.  
  45. >while(!feof(handle)) {
  46.  
  47. feof() in C does not require the runtime system to make statements 
  48. about the future. The "end of file" condition on a stream will be
  49. true _after_ trying to read beyond the end of the file, not before
  50. doing so. The FAQ for this newsgroup could have told you that.
  51.  
  52. >    n = 0;
  53. >    fp = fgets(data, 40, handle);
  54.  
  55. fgets() does return a pointer to the supplied buffer in case of 
  56. success. It does return something different if the end of the FILE
  57. has been reached. The documentation that comes with your library
  58. could have told you that.
  59.  
  60. >    if ( strcasecmp(fp, "<action>\n") == 0)  /* the coredump is here */
  61.  
  62. Using a NULL pointer as a parameter to strcmp() - I will ignore the "
  63. case" for the moment - will lead to undesirable results on some 
  64. systems. An operating system will issue a diagnostic message.
  65.  
  66. Kurt
  67. -- 
  68. | Kurt Watzka                             Phone : +49-89-2180-6254
  69. | watzka@stat.uni-muenchen.de
  70.